package com.example.hxds.mis.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.common.util.PageUtils;
import com.example.hxds.common.util.R;
import com.example.hxds.mis.api.controller.form.*;
import com.example.hxds.mis.api.db.dao.UserDao;
import com.example.hxds.mis.api.feign.*;
import com.example.hxds.mis.api.service.OrderService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.Length;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author XUYANG
 * @Date 2024-04-01 15:03
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderServiceApi orderServiceApi;
    @Resource
    private CstServiceApi cstServiceApi;
    @Resource
    private DrServiceApi drServiceApi;
    @Resource
    private MpsServiceApi mpsServiceApi;
    @Resource
    private NebulaServiceApi nebulaServiceApi;
    @Resource
    private RuleServiceApi ruleServiceApi;
    @Resource
    private WorkflowServiceApi workflowServiceApi;
    @Resource
    private UserDao userDao;
    @Override
    public PageUtils searchOrderByPage(SearchOrderByPageForm form) {
        R r=orderServiceApi.searchOrderByPage(form);
        HashMap map= (HashMap) r.get("result");
        PageUtils res= BeanUtil.toBean(map,PageUtils.class);
        return res;
    }

    @Override
    public HashMap searchOrderComprehensiveInfo(long orderId) {
        HashMap res=new HashMap();
        //查询订单内容信息
        SearchOrderContentForm o_form=new SearchOrderContentForm();
        o_form.setOrderId(orderId);
        R o_r=orderServiceApi.searchOrderContent(o_form);
        if (!o_r.containsKey("result")){
            throw new HxdsException("不存在该订单记录");
        }
        HashMap content=(HashMap) o_r.get("result");
        res.put("content",content);

        //查询乘客信息
        long customerId= MapUtil.getLong(content,"customerId");
        SearchCustomerBriefInfoForm c_form=new SearchCustomerBriefInfoForm();
        c_form.setCustomerId(customerId);
        R c_r=cstServiceApi.searchCustomerBriefInfo(c_form);
        HashMap customerInfo=(HashMap) c_r.get("result");
        res.put("customerInfo",customerInfo);

        //查寻司机信息
        long driverId=MapUtil.getLong(content,"driverId");
        SearchDriverBriefInfoForm d_form=new SearchDriverBriefInfoForm();
        d_form.setDriverId(driverId);
        R d_r=drServiceApi.searchDriverBriefInfo(d_form);
        HashMap driverInfo=(HashMap) d_r.get("result");
        res.put("driverInfo",driverInfo);

        //查询消费规则
        if (content.containsKey("chargeRuleId")){
            long chargeRuleId=MapUtil.getLong(content,"chargeRuleId");
            SearchChargeRuleByIdForm r_form=new SearchChargeRuleByIdForm();
            r_form.setRuleId(chargeRuleId);
            R r_r=ruleServiceApi.searchChargeRuleById(r_form);
            HashMap chargeRule=(HashMap) r_r.get("result");
            res.put("chargeRule",chargeRule);
        }

        //查询消费规则
        if (content.containsKey("cancelRuleId")){
            long cancelRuleId=MapUtil.getLong(content,"cancelRuleId");
            SearchCancelRuleByIdForm cr_form=new SearchCancelRuleByIdForm();
            cr_form.setRuleId(cancelRuleId);
            R cr_r=ruleServiceApi.searchCancelRuleById(cr_form);
            HashMap cancelRule= (HashMap) cr_r.get("result");
            res.put("cancelRule",cancelRule);
        }

        //查询分账规则
        if (content.containsKey("profitsharingRuleId")){
            long profitsharingRuleId=MapUtil.getLong(content,"profitsharingRuleId");
            SearchProfitsharingRuleByIdForm p_form=new SearchProfitsharingRuleByIdForm();
            p_form.setRuleId(profitsharingRuleId);
            R p_r=ruleServiceApi.searchProfitsharingRuleById(p_form);
            HashMap p_rule=(HashMap) p_r.get("result");
            res.put("profitsharingRule",p_rule);
        }

        //查询GPS规划线路
        CalculateDriveLineForm cd_form=new CalculateDriveLineForm();
        HashMap startPlaceLocation=(HashMap) content.get("startPlaceLocation");
        HashMap endPlaceLocation=(HashMap) content.get("endPlaceLocation");
        cd_form.setStartPlaceLatitude(MapUtil.getStr(startPlaceLocation,"latitude"));
        cd_form.setStartPlaceLongitude(MapUtil.getStr(startPlaceLocation,"longitude"));
        cd_form.setEndPlaceLatitude(MapUtil.getStr(endPlaceLocation,"latitude"));
        cd_form.setEndPlaceLongitude(MapUtil.getStr(endPlaceLocation,"longitude"));
        R cd_r=mpsServiceApi.calculateDriveLine(cd_form);
        HashMap driverLine=(HashMap) cd_r.get("result");
        res.put("driverLine",driverLine);

        int status=MapUtil.getInt(content,"status");
        if (status>=5&&status<=8){
            //查询订单GPS定位记录
            SearchOrderGpsForm g_form=new SearchOrderGpsForm();
            g_form.setOrderId(orderId);
            R g_r=nebulaServiceApi.searchOrderGps(g_form);
            ArrayList<HashMap> orderGps=(ArrayList<HashMap>) g_r.get("result");
            res.put("orderGps",orderGps);
        }else if (status==4){
            //查询订单中最后的GPS定位
            SearchOrderLastGpsForm lg_form=new SearchOrderLastGpsForm();
            lg_form.setOrderId(orderId);
            R lg_r=nebulaServiceApi.searchOrderLastGps(lg_form);
            HashMap lastGps=(HashMap) lg_r.get("result");
            res.put("lastGps",lastGps);
        }
        return res;
    }

    @Override
    public HashMap searchOrderLastGps(SearchOrderLastGpsForm form) {
        SearchOrderStatusForm s_form=new SearchOrderStatusForm();
        s_form.setOrderId(form.getOrderId());
        R r=orderServiceApi.searchOrderStatus(s_form);
        if (!r.containsKey("result")){
            throw new HxdsException("没有对应的订单记录");
        }
        int status=MapUtil.getInt(r,"result");
        if (status==4){
            r=nebulaServiceApi.searchOrderLastGps(form);
            HashMap lastGps=(HashMap) r.get("result");
            return lastGps;
        }
        return null;
    }

    @Override
    public ArrayList<HashMap> searchOrderStartLocationIn30Days() {
        R r=orderServiceApi.searchOrderStartLocationIn30Days();
        ArrayList<HashMap> list=(ArrayList<HashMap>) r.get("result");
        ArrayList<HashMap> res=new ArrayList<>();
        Map<HashMap,Integer> map= CollectionUtil.countMap(list);
        map.forEach((key,value)->{
            key.replace("latitude",MapUtil.getDouble(key,"latitude"));
            key.replace("longitude",MapUtil.getDouble(key,"longitude"));
            key.put("count",value);
            res.add(key);
        });
        return res;
    }

    @Override
    public PageUtils searchCommentByPage(SearchCommentByPageForm form) {
        R r=orderServiceApi.searchCommentByPage(form);
        HashMap map=(HashMap) r.get("result");
        PageUtils res=BeanUtil.toBean(map,PageUtils.class);
        return res;
    }

    @Override
    @Transactional
    @LcnTransaction
    public void acceptCommentAppeal(AcceptCommentAppealForm form) {
        HashMap map=userDao.searchUserSummary(form.getUserId());
        String userName=MapUtil.getStr(map,"name");
        form.setUserName(userName);
        workflowServiceApi.acceptCommentAppeal(form);
    }

    @Override
    @Transactional
    @LcnTransaction
    public void handleCommentAppeal(HandleCommentAppealForm form) {
        workflowServiceApi.handleCommentAppeal(form);
    }

    @Override
    public HashMap searchAppealContent(SearchAppealContentForm form) {
        R r=workflowServiceApi.searchAppealContent(form);
        HashMap map=(HashMap) r.get("result");
        return map;
    }
}
